﻿using Autofac;
using Autofac.Extras.DynamicProxy;
using AutoMapper;
using MDT.LifeSewagePlatform.Conmmon;
using MDT.LifeSewagePlatform.Entity;
using MDT.LifeSewagePlatform.Extension;
using MDT.LifeSewagePlatform.Mysql;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace MDT.LifeSewagePlatform.Configuration
{
    public static  class AutofaceConfigExtension
    {
        public static void RegistService(this ContainerBuilder containerBuilder)
        {
            string path = AppDomain.CurrentDomain.BaseDirectory;
            Assembly assIRes = Assembly.LoadFrom(System.IO.Path.Combine(path, "MDT.LifeSewagePlatform.IRepository.dll"));
            Assembly assRes = Assembly.LoadFrom(System.IO.Path.Combine(path, "MDT.LifeSewagePlatform.Repository.dll"));

            Assembly assIservice = Assembly.LoadFrom(System.IO.Path.Combine(path, "MDT.LifeSewagePlatform.InterfaceService.dll"));
            Assembly assservice = Assembly.LoadFrom(System.IO.Path.Combine(path, "MDT.LifeSewagePlatform.Service.dll"));

            containerBuilder.RegisterAssemblyTypes(assRes, assIRes).AsImplementedInterfaces().Where(r=>r.FullName.Contains("Repository")).PropertiesAutowired(new AutowiredPropertySelector()).EnableInterfaceInterceptors();
            containerBuilder.RegisterAssemblyTypes(assservice, assIservice).AsImplementedInterfaces().Where(s=>s.FullName.Contains("Service")).PropertiesAutowired(new AutowiredPropertySelector()).EnableInterfaceInterceptors();

            containerBuilder.RegisterType<MysqlBase>().As<IMysqlDBBase>().InstancePerLifetimeScope().PropertiesAutowired(new AutowiredPropertySelector());
            containerBuilder.RegisterType<Mapper>().As<IMapper>().PropertiesAutowired(new AutowiredPropertySelector());
            containerBuilder.RegisterType<ExcelHelper>().As<IExcelHelper>().PropertiesAutowired(new AutowiredPropertySelector());
            containerBuilder.RegisterType<SendPhoneNumberMsg>().As<ISendPhoneNumberMsg>().PropertiesAutowired(new AutowiredPropertySelector());
            containerBuilder.RegisterType<SendEmail>().As<ISendEmail>().PropertiesAutowired(new AutowiredPropertySelector());
            containerBuilder.RegisterType<MangerWord>().As<IManagerWord>().PropertiesAutowired(new AutowiredPropertySelector());
            containerBuilder.RegisterType<SocketHelper>().As<ISocketHelper>().PropertiesAutowired(new AutowiredPropertySelector());
            containerBuilder.RegisterType<FileHelper>().As<IFileHelper>().PropertiesAutowired(new AutowiredPropertySelector());
            containerBuilder.RegisterType<CacheTokenDictionary>().As<ICacheTokenDictionary>().SingleInstance().PropertiesAutowired(new AutowiredPropertySelector());
            containerBuilder.RegisterType<HttpHelper>().As<IHttpHelper>().PropertiesAutowired(new AutowiredPropertySelector());
            containerBuilder.RegisterType<JTokenMappingEntityService>().As<IJTokenMappingEntityService>().PropertiesAutowired(new AutowiredPropertySelector());
        }
    }
}
